format binary as 'bin'
USE16
org 8400h

include 'inc/constants.inc'

kernel:

    pusha
    xor ax, ax
    mov es, ax
    mov word [es:IVT_VIDEO], ext_video_handler
    mov word [es:IVT_VIDEO+2], cs
    mov word [es:IVT_MEMORY], ext_memory_handler
    mov word [es:IVT_MEMORY+2], cs
    mov word [es:IVT_KEYBOARD], ext_keyboard_handler
    mov word [es:IVT_KEYBOARD+2], cs
    mov word [es:IVT_DISKIO], ext_diskio_handler
    mov word [es:IVT_DISKIO+2], cs
    popa

    ; xor ah, ah
    ; int 20h

    ; mov ax, 4321
    ; mov si, num
    ; call itoa

    ; mov ah, 1
    ; int 20h

;     mov cx, 5
;     mov di, 0
;     mov si, hw
; .loop:
;     mov ah, 1
;     int 20h

;     mov bx, di
;     mov ah, 4
;     int 20h
;     inc di

;     mov ah, 2
;     int 20h

;     loop .loop
; .end:
;     mov ah, 1
;     mov si, gw
;     int 20h
    jmp $
hw db "Hello, world! ", 0
gw db "Goodbye, world!", 0
num db "1234", 0

.message db "CF changed", 0

; Convert unsigned int to string
;
; AX - Num to convert
; SI - Where save string
; ---
; SI - Pointer to string with converted num
itoa:
    push cx
    push bx
    push di
    push ax

    mov cx, 0
    mov bx, 10          ; Set BX 10, for division and mod
    mov di, si         ; Get our pointer ready

.push:
    mov dx, 0
    div bx              ; Remainder in DX, quotient in AX
    inc cx              ; Increase pop loop counter
    push dx             ; Push remainder, so as to reverse order when popping
    test ax, ax         ; Is quotient zero?
    jnz .push           ; If not, loop again
.pop:
    pop dx              ; Pop off values in reverse order, and add 48 to make them digits
    add dl, '0'         ; And save them in the string, increasing the pointer each time
    mov [di], dl
    inc di
    dec cx
    jnz .pop

    mov byte [di], 0        ; Zero-terminate string

    mov si, di
    pop ax
    pop di
    pop bx
    pop cx
    ret

include 'api/api.inc'
